package com.itheima.tlias.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.tlias.dto.AddStudentDto;
import com.itheima.tlias.dto.CheckStudentDto;
import com.itheima.tlias.dto.UpdateStudentDto;
import com.itheima.tlias.entity.Student;
import com.itheima.tlias.exception.BizException;
import com.itheima.tlias.mapper.StudentMapper;
import com.itheima.tlias.service.StudentService;
import com.itheima.tlias.vo.PageBean;
import com.itheima.tlias.vo.StudentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageBean<StudentVo> studentPage(CheckStudentDto dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<StudentVo> students = studentMapper.studentPage(dto);
        return new PageBean<>(students.getTotal(), students.getResult());
    }

    @Override
    public void deleteStudents(List<Integer> ids) {

        studentMapper.deleteStudents(ids);

    }

    @Override
    public void addStudent(AddStudentDto dto) {
        if (studentMapper.studentByNo(dto.getNo()) != null) {
            throw new BizException("学号重复");
        }
        if (studentMapper.studentByPhone(dto.getPhone()) != null) {
            throw new BizException("手机号重复");
        }
        if (studentMapper.studentByIdCard(dto.getIdCard()) != null) {
            throw new BizException("身份证号重复");
        }
        Student student = new Student();
        BeanUtils.copyProperties(dto, student);
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.addStudent(student);
    }

    @Override
    public StudentVo getById(Integer id) {
        return studentMapper.getById(id);
    }


    @Override
    public void updateStudent(UpdateStudentDto dto) {
        if (studentMapper.studentByNo(dto.getNo()) != null &&
                !Objects.equals(studentMapper.studentByNo(dto.getNo()).getId(), dto.getId())) {
            throw new BizException("学号重复");
        }
        if (studentMapper.studentByPhone(dto.getPhone()) != null &&
                !Objects.equals(studentMapper.studentByPhone(dto.getPhone()).getId(), dto.getId())) {
            throw new BizException("手机号重复");
        }
        if (studentMapper.studentByIdCard(dto.getIdCard()) != null &&
                !Objects.equals(studentMapper.studentByIdCard(dto.getIdCard()).getId(), dto.getId())) {
            throw new BizException("身份证号重复");
        }
        Student student = new Student();
        BeanUtils.copyProperties(dto, student);
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.updateStudent(student);
    }

    @Override
    public void violateDiscipline(Integer id, Integer score) {
        if (score == null || score <= 0) {
            throw new BizException("数据非法");
        }
        studentMapper.violateDiscipline(id, score);
    }
}
